<template>
    <div>
  <el-breadcrumb separator-class="el-icon-arrow-right">
  <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
  <el-breadcrumb-item>用户管理</el-breadcrumb-item>
  <el-breadcrumb-item>用户列表</el-breadcrumb-item>
</el-breadcrumb> <br>
<el-card>
   <el-input placeholder="输入搜索内容" v-model="user.query" @input="search"></el-input>
   <el-button @click="search">搜索</el-button>
   <el-button type="primary" @click="add">添加</el-button> <br> <br>

  <el-table :data="tableData"   border style="width: 100%">
    <el-table-column type="index"  label="#"> </el-table-column>
    <el-table-column prop="username"  label="姓名"> </el-table-column>
    <el-table-column  prop="email"  label="邮箱"></el-table-column>
    <el-table-column prop="mobile"  label="电话"> </el-table-column>
    <el-table-column  prop="role_name"  label="角色"></el-table-column>
    <el-table-column prop="mg_state"  label="状态"> 
         <template slot-scope="scope">
             <el-switch 
            v-model="scope.row.mg_state"
             @change="xiustate(scope.row)"
            >
               </el-switch>
         </template>
    </el-table-column>
    <el-table-column  prop="name"  label="操作" width="260">
         <template slot-scope="scope"> 
            <el-button type="primary" size="mini" @click="bianji(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
            <el-button type="warning" size="mini" @click="fpjs(scope.row)">设置</el-button>
         </template>
    </el-table-column>
  </el-table>
</el-card>
<!-- 分页 -->
 <div class="block">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="user.pagenum"
      :page-sizes="[1, 2, 3, 4]"
      :page-size="user.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
<!-- 添加模态框 -->
<el-dialog title="添加用户" :visible.sync="dialogFormVisible">
  <el-form :model="addform" :rules="rules" ref="formName">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="addform.username"></el-input>
    </el-form-item> 
    <el-form-item label="密码" prop="password"> 
      <el-input v-model="addform.password"></el-input> 
    </el-form-item>
    <el-form-item label="邮箱" prop="email"> 
      <el-input v-model="addform.email"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="mobile">
      <el-input v-model="addform.mobile"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="adduser">确 定</el-button>
  </div>
</el-dialog>
<!-- 修改的模态框 -->
<el-dialog title="修改用户" :visible.sync="dialogFormVisibles">
  <el-form :model="addforms" :rules="rules" ref="formName">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="addforms.username" disabled></el-input>
    </el-form-item> 
    <el-form-item label="邮箱" prop="email"> 
      <el-input v-model="addforms.email"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="mobile">
      <el-input v-model="addforms.mobile"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisibles = false">取 消</el-button>
    <el-button type="primary" @click="addbianjia">确 定</el-button>
  </div>
</el-dialog>
<!-- 分配角色的模态框 -->
<el-dialog title="分配角色" :visible.sync="dialogFormVisibless">
  <el-form :model="form"> 
    <el-form-item label="当前用户名" > 
      <span>{{form.username}}</span>
    </el-form-item> 
    <el-form-item label="当前角色" >
       <span>{{form.role_name}}</span>
    </el-form-item>
    <el-form-item label="分配角色" >
         <el-select  placeholder="请选择角色" v-model="rid">
        <el-option   v-for="item in Roles" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
      </el-select>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisibless = false">取 消</el-button>
    <el-button type="primary"  @click="tjjs">确 定</el-button>
  </div>
</el-dialog>





    </div>
</template>
<script>
import {
  getusersApi,
  adduserApi,
  deluserApi,
  bianuserApi,
  xiuuserApi,
  getrolesApi,
  jueseuserApi
} from "@/http/api";
export default {
  data() {
    return {
      tableData: [], //表格的数据
      total: 0,
      value: 1,
      rid:'',   //分配角色的双向数据绑定
      Roles:{},  //分配角色的数据
      user: {
        //渲染必须传的参数
        query: "",
        pagenum: 1,
        pagesize: 4
      },
      dialogFormVisible: false, //添加内容的模态框
      dialogFormVisibles: false, //修改的模态框
      dialogFormVisibless:false,  //分配角色的对话框
      addform: {
        //添加的内容
        username: "",
        password: "",
        email: "",
        mobile: ""
      },
      addforms: {
        //修改的内容
        username: "",
        password: "",
        email: "",
        mobile: "",
        id: 0
      },
      form:{
         
      },
      //表单校验
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [{ required: true, message: "请输入邮箱", trigger: "blur" }],
        mobile: [{ required: true, message: "请输入手机号", trigger: "blur" }]
      }
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    //表格的数据
    async getlist() {
      let res = await getusersApi(this.user);
      console.log(res);
      this.tableData = res.users;
      let { total } = res;
      this.total = total;
    },
    handleSizeChange(val) {
      this.user.pagesize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      this.user.pagenum = val;
      this.getlist();
    },
    //搜索
    search() {
       console.log('search',this.user)
       this.getlist()
    },
    //点击添加按钮出现模态框
    add() {
      this.dialogFormVisible = true;
    },
    //点击添加添加内容
    adduser() {
      this.$refs.formName.validate(async valid => {
        if (valid) {
          await adduserApi(this.addform);
          this.dialogFormVisible = false;
          this.getlist();
        } else {
          //请求添加的接口
          return false;
        }
      });
    },
    //删除
    del(id) {
      console.log(id);
      this.$confirm("是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          await deluserApi(id);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.getlist();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //点击编辑出现模态框  数据回填
    bianji(row) {
      console.log(row);
      this.dialogFormVisibles = true;
      this.addforms = row; //数据回填
    },
    //提交编辑
    addbianjia() {
      this.$refs.formName.validate(async valid => {
        if (valid) {
          let { id, mobile, email } = this.addforms; //先将值赋给修改的表单
          let res = await bianuserApi(id, { mobile, email });
          console.log(res);
          this.dialogFormVisibles = false;
          this.getlist();
        } else {
          //请求添加的接口
          return false;
        }
      });
    },
    //修改状态
    async xiustate(row) {
      console.log(row);
      let uId = row.id;
      let type = row.mg_state;
      await xiuuserApi(uId, type);
    },
    //分配角色
  async  fpjs(row){
        console.log(row) 
        this.dialogFormVisibless=true
        this.form=row  //数据回填
        //请求角色列表的接口
        let res=await getrolesApi()
        console.log(res,'角色列表') 
        this.Roles=res 
    },
    //提交分配角色
  async  tjjs(){
       console.log(this.rid,this.form.id) 
       //请求分配角色接口
       await jueseuserApi(this.form.id,this.rid)
       this.dialogFormVisibless=false
       this.getlist() 
    }
  }
};
</script>

<style scoped >
.el-input {
  width: 350px;
}
</style>
